# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: reporter\reports\quick.py
from reporter.reports.basic_template import NamedTempFileWithNormalizedWrite
from reporter.reports.basic_template import ReportTemplate
from io import StringIO
import jinja2


class QuickReport(ReportTemplate):
    REPORT_NAME = "Quick Report"

    def parts(self):
        template_loader = jinja2.FileSystemLoader(searchpath="data/templates/reports/")
        template_env = jinja2.Environment(loader=template_loader)
        template_vars = {}
        template = template_env.get_template("quick_template.html")
        common_parts_template_vars = {}
        common_parts_template = template_env.get_template("common_parts_template.html")
        data = StringIO()
        data.write(common_parts_template.render({"template_part": "header"}))
        common_parts_template_vars["template_part"] = "body_start"
        common_parts_template_vars["report_name"] = self.REPORT_NAME
        common_parts_template_vars["date_generated"] = self.get_formatted_nowdate()
        data.write(common_parts_template.render(common_parts_template_vars))
        data.seek(0)
        yield data
        data = StringIO()
        with NamedTempFileWithNormalizedWrite() as (file_alerts):
            for source in self.sources:
                alerts_count = {3: 0, 2: 0, 1: 0, 0: 0}
                file_alerts.seek(0)
                file_alerts.truncate()
                file_alerts.write(template.render({"template_part": "alerts_title"}))
                for vulnerability_type, cnt in source.vulnerability_types():
                    alerts_count[vulnerability_type["severity"]] += cnt
                    template_vars["vulnerability_type"] = vulnerability_type
                    for vulnerability, location, _ in source.vulnerabilities(
                        vtype=vulnerability_type
                    ):
                        vulnerability["request"] = self.escape_html(
                            vulnerability.get("request") or ""
                        )
                        vulnerability["location"] = location
                        template_vars["vulnerability"] = vulnerability
                        template_vars["template_part"] = "alert"
                        file_alerts.write(template.render(template_vars))

                common_parts_template_vars["template_part"] = "display_scan_info"
                common_parts_template_vars[
                    "threat_level"
                ] = self.calculate_threat_level(alerts_count)
                common_parts_template_vars[
                    "scan_info"
                ] = self.get_well_formed_scan_info(source)
                common_parts_template_vars["alerts_count"] = alerts_count
                data.write(common_parts_template.render(common_parts_template_vars))
                data.seek(0)
                yield data
                data = StringIO()
                file_alerts.seek(0)
                yield file_alerts

        data.write(common_parts_template.render({"template_part": "body_end"}))
        data.seek(0)
        yield data
